<script setup lang="ts">
import { GoodsListVo } from '@/models/goods/GoodsListVo';
import { reactive, onMounted } from 'vue';
import { toMoney } from "@/utils/utils"
import { getGoodsList } from '@/api/goods/GoodsApi'
import { buyProduct } from '@/api/order/OrderApi'
import { EnumPlatform } from '@/models/enums/EnumPlatform';
import wx from 'weixin-js-sdk'
import { showNotify } from 'vant';
type GoodsListPageViewData = {
  productList: GoodsListVo[];
}
const goodsListPageViewData = reactive<GoodsListPageViewData>({
  productList: []
});

const initData = async () => {
  goodsListPageViewData.productList = await getGoodsList(null);
};
onMounted(initData)


const buy = async (id: string) => {
  let createOrderResult = await buyProduct(id, EnumPlatform.Wechat);
  console.log(createOrderResult);
  if (createOrderResult && createOrderResult.resultData) {
    debugger;
    wx.config({
      debug: true,
      appId: createOrderResult.resultData.appId,
      timestamp: parseInt(createOrderResult.resultData.timeStamp),
      nonceStr: createOrderResult.resultData.nonceStr,
      signature: "SHA256",
      jsApiList: ["chooseWXPay"],
    });

    wx.ready(function () {
      {
        wx.chooseWXPay({
          timestamp: parseInt(createOrderResult.resultData?.timeStamp ?? ""),
          nonceStr: createOrderResult.resultData?.nonceStr ?? "",
          //signature: createOrderResult.resultData?.paySign??"",
          paySign: createOrderResult.resultData?.paySign ?? "",
          signType: createOrderResult.resultData?.signType ?? "",
          package: createOrderResult.resultData?.package ?? "",
          success(res) {
            console.log(res);
            showNotify({ type: 'success', message: '通知内容',duration:1500 });
            setTimeout(()=>{},1500);
          }
        });
      }
    });
  }
};

</script>

<template>
  <van-card v-for="item in goodsListPageViewData.productList" :thumb="(item.converImg)" :key="item.id"
    :price="toMoney(item.buyPrice)" :desc="item.goodsDesc" :title="item.goodsName"
    :origin-price="toMoney(item.originPrice)">
    <template #footer>
      <van-button size="normal" @click="buy(item.id)">购买</van-button>
    </template>
  </van-card>
</template>